<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="居民姓名" prop="elderName">
        <el-input
          v-model="queryParams.elderName"
          placeholder="请输入居民"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间名称" prop="nurseId">
        <el-input
          v-model="queryParams.roomCode"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:beds:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bedsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房间类型" align="center" prop="roomType" />
      <el-table-column label="房间号" align="center" prop="roomCode" />
      <el-table-column label="床位编码" align="center" prop="bedCode" />
      <el-table-column label="公寓" align="center" prop="apartmentName" />
      <el-table-column label="居民" align="center" prop="elderName" />
      <el-table-column label="床位状态" align="center" prop="bedStatus" />
      <el-table-column label="备注" align="center" prop="notes" />
      <el-table-column label="负责护工" align="center" prop="nurseId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:beds:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改床位信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body center="true">
      <el-form ref="form" :model="form" :rules="rules" label-width="200px" >
        <el-row>
          <el-form-item label="床位编码" prop="bedCode">
            <el-input v-model="form.bedCode" :disabled="isDisabledbedCode" placeholder="请输入床位编码" style="width: 150px;" />
          </el-form-item>
<!--            <el-form-item label="公寓名称" prop="apartmentId">-->
<!--              <el-select v-model="form.apartmentId" :disabled="isDisabledApartmentName" clearable placeholder="请选择" style="width: 150px">-->
<!--                <el-option-->
<!--                  v-for="item in apartmentList"-->
<!--                  :key="item.apartmentId"-->
<!--                  :label="item.apartmentName"-->
<!--                  :value="item.apartmentId">-->
<!--                </el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
          <el-form-item label="房间名称" prop="roomId">
            <el-select v-model="form.roomId" :disabled="isDisabledRoomName" clearable placeholder="请选择" style="width: 150px">
              <el-option
                v-for="item in roomInfoList"
                :key="item.roomId"
                :label="item.roomType+':'+item.roomCode"
                :value="item.roomId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="房间状态" prop="bedStatus">
            <el-select v-model="form.bedStatus" placeholder="请选择" style="width: 100px">
              <el-option label="空闲" value="301"></el-option>
              <el-option label="已住" value="302"></el-option>
              <el-option label="暂离" value="303"></el-option>
              <el-option label="维护" value="304"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="居民名称" prop="elderId">
            <el-input v-model="form.elderName" placeholder="请输入居民" style="width: 200px"/>
          </el-form-item>
          <el-form-item label="备注" prop="notes">
            <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" style="width: 200px"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBeds, getBeds, delBeds, addBeds, updateBeds } from "@/api/business/beds";
import {getElderFamilies} from "@/api/business/elderFamilies";
import {getElderApartment} from "@/api/business/elderInfo";
import {getRoomInfoList} from "@/api/business/roomInfo";

export default {
  name: "Beds",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 床位信息表格数据
      bedsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        apartmentId: null,
        roomId: null,
        bedCode: null,
        elderId: null,
        elderName:null,
        bedStatus: null,
        roomCode: null,
        nurseId: null,
        createUserId: null,
        updateUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      apartmentList:{},
      roomInfoList:{},
    };
  },
  created() {
    this.getList();
    this.getElderApartmentInfo();
    this.GetRoomInfoList();
  },
  computed: {
    // isDisabledbedCode() {
    //   return this.form.bedCode; // 如果input有值，则禁用，否则不禁用
    // },
    // isDisabledApartmentName() {
    //   return this.form.apartmentName; // 如果input有值，则禁用，否则不禁用
    // },
    // isDisabledRoomName() {
    //   return this.form.roomCode; // 如果input有值，则禁用，否则不禁用
    // },
  },
  methods: {
    //获取公寓信息
    getElderApartmentInfo(){
      getElderApartment().then(response =>{
        this.apartmentList = response.rows
      })
    },
    /** 查询床位信息列表 */
    getList() {
      this.loading = true;
      listBeds(this.queryParams).then(response => {
        this.bedsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        apartmentId: null,
        roomId: null,
        bedCode: null,
        elderId: null,
        bedStatus: null,
        notes: null,
        nurseId: null,
        createUserId: null,
        createTime: null,
        updateUserId: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加床位信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBeds(row).then(response => {
        this.form = response;
        this.open = true;
        this.title = "修改床位信息";
      });
      // getBeds(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改床位信息";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBeds(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBeds(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除床位信息编号为"' + ids + '"的数据项？').then(function() {
        return delBeds(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/beds/export', {
        ...this.queryParams
      }, `beds_${new Date().getTime()}.xlsx`)
    },
    GetRoomInfoList(){
      getRoomInfoList().then(response => {
        this.roomInfoList = response.rows;
      })
    }
  }
};
</script>
